﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM

using System.Collections.Generic;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity.OrderFlowManage;
using YunSpace.Domain.IRepository.OrderFlowManage;
using YunSpace.Domain.ViewModel.OrderFlowManage;

#endregion

namespace YunSpace.Repository.OrderFlowManage
{
    public class ClientOrdersigncontractRepository : RepositoryBase<ClientOrdersigncontractEntity>, IClientOrdersigncontractRepository
    {
        /// <summary>
        /// 获取合同签订列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public List<ClientOrderSignContractDto> GetClientOrderSignContractDtos(Pagination pagination, string orderId,string userId,string F_No)
        {
            var sql = $@"
                SELECT c.F_Id, c.F_OrderId,date_format(c.F_StartDate, '%Y-%m-%d') F_StartDate,date_format(c.F_EndDate, '%Y-%m-%d') F_EndDate,  f.F_State, c.F_Remark,
                    s.title AS F_SiteName, s2.title AS F_SpaceName,c.F_FileName,c.F_FilePath
                    FROM Site_Client_OrderSignContract AS c
                    INNER JOIN SL_Site AS s ON s.F_Id = c.F_SiteId
                    INNER JOIN SL_Space AS s2 ON s2.F_Id = c.F_SpaceId
                    inner join Sys_CheckRecord d on c.F_OrderId=d.F_OrderId and c.F_No=d.F_No
                    left join Site_Client_OrderMapping f on c.F_Id=f.F_DetailId and f.F_ApplyType=d.F_ApplyType and f.F_CheckUserId=d.F_CheckUserId
                WHERE c.F_OrderId = '{orderId}' and d.F_CheckUserId='{userId}' and d.F_ApplyType=7 and c.F_No='{F_No}'";

            pagination.records = GetClientOrderSignContractDtoCount(orderId, userId, F_No);

            var startNo = pagination.rows * (pagination.page - 1);
            var endNo = startNo + pagination.rows;

            sql = $"{sql} ORDER BY c.{pagination.sidx} LIMIT {startNo},{endNo};";

            using (var db = new RepositoryBase())
            {
                var result = db.FindList<ClientOrderSignContractDto>(sql);
                return result;
            }
        }

        /// <summary>
        /// 获取合同签订列表总数
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        private int GetClientOrderSignContractDtoCount(string orderId,string F_CheckUserId, string F_No)
        {
            var sql = $@"
                SELECT COUNT(1)
                    FROM Site_Client_OrderSignContract AS c
                    INNER JOIN SL_Site AS s ON s.F_Id = c.F_SiteId
                    INNER JOIN SL_Space AS s2 ON s2.F_Id = c.F_SpaceId
                    inner join Sys_CheckRecord d on c.F_OrderId=d.F_OrderId and c.F_No=d.F_No
                    left join Site_Client_OrderMapping f on c.F_Id=f.F_DetailId and f.F_ApplyType=d.F_ApplyType and f.F_CheckUserId=d.F_CheckUserId
                WHERE c.F_OrderId = '{orderId}' and d.F_CheckUserId='{F_CheckUserId}' and d.F_ApplyType=7 and c.F_No='{F_No}'";

            return Count(sql);
        }
    }
}

